<?php
namespace app\admin\controller;

use third\Data;
use think\Db;

class Content extends Base{

       /*
        * 文章列表
        */
    public function index(){
                //分类列表
        $catList = Db::name('category')->order('cat_id asc')->select();
        $catList =  Data::tree($catList, 'cat_name', 'cat_id', 'parent_id');

           //文章列表
        $where = '1 = 1 ';
        $cat_id = I('get.cat_id');
       if(!empty($cat_id) && $cat_id != 0){
           $where .= 'and cat_id=' . $cat_id; //根据栏目查找该栏目下的所有文章
       }
        $list = Db::name('article')->where($where)
                                   ->order('list_order desc, id desc')
                                   ->paginate(12, false, ['query' => ['cat_id' => I('get.cat_id', '')]]);
        $this->assign('cat_id', $cat_id);
        $this->assign('catList', $catList);
        $this->assign('list', $list);
        return $this->fetch();
    }

          /*
           * 添加文章
           */
       public function addArticle(){
           if(IS_POST){
              $article  = I("post.");
              $result = model('Article')->addArticleLogic($article); //添加文章
              $data = $result ? array('status' => 1, 'msg' => '添加成功！')
                              : array('status' => 1, 'msg' => '添加失败！');

                          ajaxReturn($data);
           }else{
               $list = Db::name('category')->order('cat_id asc')->select(); //分类
               $tagList = Db::name('tag')->select(); //标签
               $poList = Db::name('position')->order('po_id desc')->select(); //推荐位
               $list =  Data::tree($list, 'cat_name', 'cat_id', 'parent_id'); //生成分类文档树
               $this->assign('list', $list);
               $this->assign('tagList', $tagList);
               $this->assign('poList', $poList);
               return $this->fetch('add_article');
           }
       }

        /*
         *删除文章
         */
        public function delArticle(){
            if(IS_AJAX){
                $id = I('get.id');
                if(empty($id)){
                    $data = array('status' => 0, 'msg' => '该文章不存在！');
                       return ajaxReturn($data);
                }

                $result = Db::name('article')->where('id', 'in', $id)->delete();

                $data = $result ? array('status' => 1, 'msg' => '删除成功！')
                                : array('status' => 1, 'msg' => '删除失败！');

                        return ajaxReturn($data);
            }
        }

         /*
          *修改文章
          */
          public function editArticle(){
              if(IS_GET){
                  $id = I('get.id');
                  $info = Db::name('article')->where(array('id' => $id))->find(); //修改的文章
                  $tagList = Db::name('tag')->select(); //标签
                  $list = Db::name('category')->order('cat_id desc')->select(); //文章分类
                  $poList = Db::name('position')->order('po_id desc')->select(); //推荐位
                  $list =  Data::tree($list, 'cat_name', 'cat_id', 'parent_id'); //生成分类文档树
                  $this->assign('list', $list);
                  $this->assign('tagList', $tagList);
                  $this->assign('poList', $poList);
                  $this->assign('info', $info);
                  return $this->fetch('edit_article');
              }
              if(IS_AJAX){
                  $id = I('get.id'); //文章id
                  $article = I('post.'); //修改内容
                  $result = model('Article')->editArticleLogic($id, $article); //文章修改
                  $data = $result ? array('status' => 1, 'msg' => '修改成功！')
                                  : array('status' => 0, 'msg' => '修改失败！');

                                 return ajaxReturn($data);
              }
          }
             /*
              * 文章排序
              */
             public function orderArticle(){
                  if(IS_AJAX){
                      $list_order = I('list_order');
                      $result = list_order($list_order, 'Article'); //列表排序
                      $data = $result ? array('status' => 1, 'msg' => '排序成功！')
                                      : array('status' => 0, 'msg' => '排序失败！');

                                  return ajaxReturn($data);
                  }

             }

               /*
                * 显示或不显示文章
                */
               public function isShowArticle(){
                   if(IS_AJAX) {
                        $id = I('get.id');
                        $is_show = I('get.is_show');
                        $is_show = $is_show  ? 0 : 1;
                        $result = Db::name('article')->where(array('id' => $id))
                                                     ->update(array('is_show' => $is_show));

                        $data = $result ? array('status' => 1)
                                        : array('status' => 0);

                                  ajaxReturn($data);
                   }
               }

        /*
         * 分类列表
         */
       public function  articleCat(){
           $list = Db::name('category')->order('list_order asc, cat_id asc')->select();
           $list =  Data::tree($list, 'cat_name', 'cat_id', 'parent_id');
           $this->assign('list', $list);
          return $this->fetch('article_cat');
   }

   /*
    * 添加分类
    */
   public function addCat(){
       if(IS_POST){
           $cat = I('post.');
           if($cat['parent_id'] > 0 ){
               $level = Db::name('category')->field('cat_level')
                                            ->where(array('cat_id' => $cat['parent_id']))
                                            ->find();
               $cat['cat_level'] = (int)$level['cat_level'] + 1;
           }
           $result = Db::name('category')->insert($cat);

           $data = $result ? array('status' => 1, 'msg' => '添加成功！')
                           : array('status' => 1, 'msg' => '添加失败！');

                      ajaxReturn($data);
       }else{
           $list = Db::name('category')->order('cat_id desc')->select();
           $list =  Data::tree($list, 'cat_name', 'cat_id', 'parent_id');//分类树形结构
           $this->assign('list', $list);
           return $this->fetch('add_cat');
       }
   }
    /*
     * 修改分类
     */
    public function editCat(){
            if(IS_AJAX){
                $cat_id = I('get.cat_id');
                $cat = I('post.');
                if($cat ['parent_id'] == $cat_id) $this->error("不能将自身作为上级菜单");
                $result =  Db::name('category')->where(array('cat_id' => $cat_id))->update($cat);

                $data = $result ? array('status' => 1, 'msg' => '修改成功！')
                                : array('status' => 0, 'msg' => '修改失败！');

                   ajaxReturn($data);
            }else{
                $cat_id = I('get.cat_id');
                $info = Db::name('category')->where(array('cat_id' => $cat_id))->find();
                if($info){
                    $list = Db::name('category')->order('cat_id desc')->select();
                    $list =  Data::tree($list, 'cat_name', 'cat_id', 'parent_id');
                    $this->assign('list', $list);//菜单选项
                    $this->assign('info', $info);
                    return $this->fetch('edit_cat');
                }else{
                    $this->error('该分类不存在',U('Content/addCat'));
                }
            }
    }

   /*
    * 删除分类
    */
    public function delCat(){
        if(IS_AJAX){
                $cat_id = I('get.cat_id');
                $count = Db::name('category')->where(array('parent_id' => $cat_id))->count();
                if($count > 0){
                    $data = array('status' => 0, 'msg' => '存在子分类，无法删除！');
                }else{
                    $result = Db::name('category')->where(array('cat_id' => $cat_id))->delete();
                    $data = $result ? array('status' => 1, 'msg' => '删除成功')
                                    : array('status' => 0, 'msg' => '删除失败');
                    }

                      ajaxReturn($data);
            }
    }

               /*
                * 分类排序
                */
    public function orderCat(){
        if(IS_AJAX){
            $list_order = I('list_order');
            $result = list_order($list_order, 'Category', 'cat_id'); //列表排序
            $data = $result ? array('status' => 1, 'msg' => '排序成功！')
                            : array('status' => 0, 'msg' => '排序失败！');

            return ajaxReturn($data);

        }

    }
                  /*
                   * 标签列表
                   */
    public function tag(){
        $list = Db::name('tag')->paginate(12);
        $this->assign('list', $list);
          return $this->fetch();
    }

        /*
         * 添加标签
         */
    public function addTag(){
        if(IS_POST){
            $tag = I('post.');
            $tag['add_time'] = time();
            $result = Db::name('tag')->insert($tag);

            $data = $result ? array('status' => 1, 'msg' => '添加成功！')
                            : array('status' => 1, 'msg' => '添加失败！');
              ajaxReturn($data);
        }else{
            return $this->fetch('add_tag');
        }
    }
    /*
     * 删除标签
     */
    public function delTag(){
        if(IS_AJAX){
            $tag_id = I('get.tag_id');

                $result = Db::name('tag')->where(array('tag_id' => $tag_id))->delete();
                $data = $result ? array('status' => 1, 'msg' => '删除成功')
                                : array('status' => 0, 'msg' => '删除失败');

                      ajaxReturn($data);
        }
    }
    /*
   * 修改分类
   */
    public function editTag(){
        if(IS_AJAX){
            $tag_id = I('get.tag_id');
            $tag = I('post.');
            $result =  Db::name('tag')->where(array('tag_id' => $tag_id))->update($tag);

            $data = $result  ? array('status' => 1, 'msg' => '修改成功！')
                             : array('status' => 0, 'msg' => '修改失败！');

                   ajaxReturn($data);
        }else{
            $tag_id = I('get.tag_id');
            $info = Db::name('tag')->where(array('tag_id' => $tag_id))->find();
            if($info){
                $this->assign('info', $info);
                return $this->fetch('edit_tag');
            }else{
                $this->error('该分类不存在',U('Content/addTag'));
            }
        }
    }

    /*
     * 上传文章配图
      */
    public function upload(){
        // 获取表单上传文件 例如上传了001.jpg
        $file = request()->file('file');
        // 移动到框架应用根目录/public/uploads/ 目录下
        $info = $file->move(ROOT_PATH . 'public' . DS . 'upload/article/image');
        if($info){
            // 输出 20160820/42a79759f284b767dfcb2a0197904287.jpg
      echo $info->getSaveName();

        }else{
            // 上传失败获取错误信息
            echo $file->getError();
        }
    }
}
